/*
 * RegisterFile.java
 * 
 * Team Dovahkiin:
 * Major Bruno, Matthew Selfors
 * 
 * RegisterFile.java is a simulation of the set of 16 registers from the SC-2.
 */

package phaseI;

/**
 * This class is a simulation of the set of 16 registers from the SC-2.
 * 
 * @author Major Bruno
 * @author Matthew Selfors
 * @version 1.0
 */
public class RegisterFile {

	/**
	 * Number of registers.
	 */
	private static final int REG_COUNT = 16;

	/**
	 * An array to hold the registers.
	 */
	private Register[] registers;

	/**
	 * Calls super() and initializes the value
	 * of the registers to random numbers.
	 */
	public RegisterFile(final String[] values) {
		super();
		registers = new Register[REG_COUNT];

		for (int i = 0; i < REG_COUNT; i++) {
			registers[i] = new Register(values[i]);
		}
	}
	
	/**
	 * Returns a register at the given index.
	 * 
	 * @param reg the index of the register.
	 * @return the register.
	 */
	public Register getRegister(final int reg) {
		return registers[reg];
	}

	/**
	 * Returns a byte, either the high or low byte, depending on the
	 * parameter pick, and the register reg.
	 * 
	 * @param reg the register to get data from.
	 * @param pick the byte to get.
	 * @return the chosen byte.
	 */
	public String getByte(final int reg, final int pick) {
		String result;

		if (pick == 0) {
			result = registers[reg].getLowByte();
		} else {
			result = registers[reg].getHighByte();
		}

		return result;
	}

	/**
	 * Sets a byte, either the high or low byte, depending on the
	 * parameter pick, and the register reg.
	 * 
	 * @param reg the register to put data in.
	 * @param pick the byte to overwrite.
	 */
	public void setByte(final int reg, final int pick, final String value) {
		if (pick == 0) {
			registers[reg].setLowByte(value);
		} else if (pick == 1) {
			registers[reg].setHighByte(value);
		} else {
			registers[reg].setRegValue(value);
		}
	}

	/**
	 * Gets the value of the given register.
	 * 
	 * @param reg the register.
	 * @return that register's value.
	 */
	public String getWord(final int reg) {
		return registers[reg].getRegValue();
	}

	/**
	 * Sets the given register to the given value.
	 * 
	 * @param reg the register.
	 * @param value the value.
	 */
	public void setWord(final int reg, final String value) {
		registers[reg].setRegValue(value);
	}
}
